package cn.lyjuan.first.hadoop.demo.ch06;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;

import java.io.IOException;

public class ConnUtil {
    private static Connection conn = null;

    /**
     * 获取连接
     *
     * @return
     */
    public static Connection conn() {
        if (null != conn && !conn.isClosed())
            return conn;

        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.master.hostname", "192.168.1.200");
        conf.set("hbase.master.port", "16000");
        conf.set("hbase.rootdir", "hdfs://192.168.1.200:9000/hbase");
        conf.set("hbase.zookeeper.quorum", "hadoop0");

        try {
            conn = ConnectionFactory.createConnection(conf);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return conn;
    }

    /**
     * 关闭连接
     */
    public static void close() {
        if (null != conn && !conn().isClosed()) {
            try {
                conn.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
